package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.RoleResources;
import org.kjtc.security.MySecurityMetadataSource;
import org.kjtc.service.RoleResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleResourceController extends BaseController{

	@Resource(name = "roleResourceService")
	private RoleResourceService roleResourceService;

	@Resource(name = "mySecurityMetadataSource")
	private MySecurityMetadataSource mySecurityMetadataSource;

	@RequestMapping(value = "/searchRoleResource")
	@ResponseBody
	public Map<String, Object> searchRoleResource(RoleResources roleResource){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限查询开始");

		List<RoleResources> roleResourceList = null;
		try {
			roleResourceList = roleResourceService.getRoleResourcePage(roleResource);
			map.put("roleResourceList", roleResourceList);
			mySecurityMetadataSource.reloadResourceDefine();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限查询异常" + e.getMessage());
			return map;
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限查询结束");
		return map;
	}

	@RequestMapping(value = "/saveRoleResources")
	@ResponseBody
	public Map<String, Object> saveRoleResources(RoleResources roleResource, String saveResourceID, String deleteResourceID){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限修改开始");

		int countInsert = 0;
		int countDelete = 0;
		try {
			String [] saveResourceIDList = saveResourceID.split(",");
			roleResource.setResourcesIDList(saveResourceIDList);
			roleResource.setCreateUser(this.getUesr().getUserID());
			roleResource.setUpdateUser(this.getUesr().getUserID());
			countInsert = roleResourceService.insertRoleResources(roleResource);
			String [] deleteResourceIDList = deleteResourceID.split(",");
			roleResource.setResourcesIDList(deleteResourceIDList);
			countDelete = roleResourceService.deleteRoleResources(roleResource);
			if (countInsert > 0 || countDelete  > 0) {
				map.put("result", "Success");
				mySecurityMetadataSource.reloadResourceDefine();
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限修改异常" + e.getMessage());
			return map;
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "用户权限修改结束");
		return map;
	}
}
